package com.example.safeassistan.home.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.adapter.RiskDetailInstructionFragmentAdapter;
import com.example.safeassistan.home.model.RiskDetailInstruction;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.risk.activity.ActivityInstructionDetail;
import com.example.safeassistan.utile.JsonUtils;
import com.example.safeassistan.widget.ViewPagerForScrollView;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RiskDetailInstructionFragment extends BaseVPFragment {
    private Context context;
    private View view;
    private RecyclerView rcView;
    private LinearLayout llNodata;

    private RiskDetailInstructionFragmentAdapter adapter;
    private List<RiskDetailInstruction.ResultDTO> data;

    private ViewPagerForScrollView pager;
    private String riskpointId="";//风险点ID


    public RiskDetailInstructionFragment(ViewPagerForScrollView pager,String riskpointId) {
        this.pager = pager;
        this.riskpointId=riskpointId;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_riskdetail_risk, container, false);
            pager.setObjectForPosition(view,2);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        data=new ArrayList<>();
        rcView=view.findViewById(R.id.rcView);
        llNodata=view.findViewById(R.id.llNodata);
    }

    public void getData(){
        Map map = new HashMap();
            map.put("riskPointId",riskpointId);//风险点id
        new HttpRequest(context).doGet(UrlConstants.queryInstructorByRisk, null, map, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), RiskDetailInstruction.class);
                        if (obj instanceof RiskDetailInstruction) {
                            RiskDetailInstruction model=(RiskDetailInstruction) obj;
                            data.clear();
                            if (model.getResult().size() > 0) {
                                llNodata.setVisibility(View.GONE);
                                data=model.getResult();
                                LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context,RecyclerView.VERTICAL,false){
                                    @Override
                                    public boolean canScrollVertically() {
                                        return false;
                                    }
                                };
                                rcView.setLayoutManager(linearLayoutManager);
                                adapter=new RiskDetailInstructionFragmentAdapter(data);
                                rcView.setAdapter(adapter);
                                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        ActivityInstructionDetail.actionStart(context,
                                                model.getResult().get(position).getPostDesc(),
                                                model.getResult().get(position).getPostContent());
                                    }
                                });
                                rcView.setNestedScrollingEnabled(false);
                            } else {
                                llNodata.setVisibility(View.VISIBLE);
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }else {
                        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }


    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    @Override
    protected void onFragmentFirstVisible() {
        getData();
    }
}
